package cn.innosmart.aq.customize.photosharelibrary.imageloader;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.innosmart.aq.R;
import cn.innosmart.aq.customize.photosharelibrary.utils.CommonAdapter;
import cn.innosmart.aq.customize.photosharelibrary.utils.ViewHolder;

public class MyAdapter extends CommonAdapter<String> {
    public void setOnclickListener(OnClickListener onclickListener) {
        this.onClickListener = onclickListener;
    }

    private OnClickListener onClickListener;
    /**
     * 用户选择的图片，存储为图片的完整路径
     */
    public static List<String> mSelectedImage = new LinkedList<String>();
    private static List<ViewHolder> holders = new ArrayList<>();
    /**
     * 文件夹路径
     */
    private String mDirPath;
    private Context mContext;

    public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
                     String dirPath) {
        super(context, mDatas, itemLayoutId);
        this.mDirPath = dirPath;
        this.mContext = context;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(mContext, "您已选择了一张图片", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void convert(final ViewHolder helper, final String item) {
        System.out.println("item=" + item);
        //设置no_pic
        helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
        //设置no_selected
        helper.setImageResource(R.id.id_item_select, R.drawable.picture_unselected);
        //设置图片
        if (item.contains("/")) {
//            if (helper.getPosition() == 0) {
//                final ImageView mImageView = helper.getView(R.id.id_item_image);
//                final ImageView mSelect = helper.getView(R.id.id_item_select);
//                mSelect.setImageDrawable(null);
//                mImageView.setImageResource(R.drawable.camera);
//                mImageView.setOnClickListener(onClickListener);
//            } else {
            helper.setImageByUrl(R.id.id_item_image, item);
            final ImageView mImageView = helper.getView(R.id.id_item_image);
            final ImageView mSelect = helper.getView(R.id.id_item_select);
            mImageView.setColorFilter(null);
            /**
             * 已经选择过的图片，显示出选择过的效果
             */
            if (mSelectedImage.contains(item)) {
                mSelect.setImageResource(R.drawable.pictures_selected);
                mImageView.setColorFilter(Color.parseColor("#77000000"));
                System.out.println("help=" + helper.getPosition());
                holders.add(helper);
            }
            //设置ImageView的点击事件
            mImageView.setOnClickListener(new OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {
                    // 已经选择过该图片
                    if (mSelectedImage.contains(item)) {
                        mSelectedImage.remove(item);
                        mSelect.setImageResource(R.drawable.picture_unselected);
                        mImageView.setColorFilter(null);
                        holders.remove(helper);
                    } else {
                        // 未选择该图片
                        for (int i = 0; i < holders.size(); i++) {
                            ((ImageView) holders.get(i).getView(R.id.id_item_image)).setColorFilter(null);
                            ((ImageView) holders.get(i).getView(R.id.id_item_select)).setImageResource(R.drawable.picture_unselected);
                        }
                        holders.clear();
                        mSelectedImage.clear();
                        mSelectedImage.add(item);
                        mSelect.setImageResource(R.drawable.pictures_selected);
                        mImageView.setColorFilter(Color.parseColor("#77000000"));
                        holders.add(helper);
                    }
                }
            });
//            }
        } else {
            helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);
            final ImageView mImageView = helper.getView(R.id.id_item_image);
            final ImageView mSelect = helper.getView(R.id.id_item_select);
            mImageView.setColorFilter(null);
            /**
             * 已经选择过的图片，显示出选择过的效果
             */
            int i = 0;
            if (mSelectedImage.contains(mDirPath + "/" + item)) {
                mSelect.setImageResource(R.drawable.pictures_selected);
                mImageView.setColorFilter(Color.parseColor("#77000000"));
                System.out.println("help=" + helper.getPosition());
                holders.add(helper);
            }
            //设置ImageView的点击事件
            mImageView.setOnClickListener(new OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {
                    // 已经选择过该图片
                    if (mSelectedImage.contains(mDirPath + "/" + item)) {
                        mSelectedImage.remove(mDirPath + "/" + item);
                        mSelect.setImageResource(R.drawable.picture_unselected);
                        mImageView.setColorFilter(null);
                        holders.remove(helper);
                    } else {
                        // 未选择该图片
                        for (int i = 0; i < holders.size(); i++) {
                            ((ImageView) holders.get(i).getView(R.id.id_item_image)).setColorFilter(null);
                            ((ImageView) holders.get(i).getView(R.id.id_item_select)).setImageResource(R.drawable.picture_unselected);
                        }
                        System.out.println("add");
                        holders.clear();
                        mSelectedImage.clear();
                        mSelectedImage.add(mDirPath + "/" + item);
                        mSelect.setImageResource(R.drawable.pictures_selected);
                        mImageView.setColorFilter(Color.parseColor("#77000000"));
                        holders.add(helper);
                    }
                }
            });
        }

    }
}
